<?php
/**
 * @author giorgi chichinadze
 *
 */

ini_set('memory_limit', '-1');

class asterisk {
    
    public $host;
    public $port;
    public $user;
    public $pass;
    
    public $timeout;
    
    public $connect;
    
    public $phone;
    public $reqdetid;
    
    public $waittime;
    
    
    /**
     * @param string $host
     * @param int $port
     * @param string $user
     * @param string $pass
     * @param int $timeout
     */
    function asterisk($host, $port, $user, $pass, $timeout) {
        
        $this->connect = @fsockopen($host, $port, $errno, $errstr, $timeout);

        if (!$this->connect) {
            
            echo "ERROR: $errstr (ERROR N: $errno)\n";
            
        } else {
        
            $login = "Action: login\r\n";
            $login .= "Username: $user\r\n";
            $login .= "Secret: $pass\r\n";
            $login .= "Events: Off\r\n";
            $login .= "\r\n";
            fwrite($this->connect,$login);
        
            $manager_version    = fgets($this->connect);
            $cmd_response       = fgets($this->connect);
            $response           = fgets($this->connect);
            $blank_line         = fgets($this->connect);
        
            if (substr($response,0,9) == "Message: ") {
                
                $loginresponse = trim(substr($response,9));
                if (!$loginresponse == "Authentication Accepted"){
                    
                    echo "-- Unable to log in: $loginresponse\n";
                    fclose($this->connect);
                    exit(0);
                    
                }
            }else{
                
                echo "Unexpected response: $response\n";
                fclose($this->connect);
                exit(0);
                
            }
        }
    }
    
    /**
     * @param string $phone
     * @param int $waittime
     */
    function makeCall($reqdetid, $phone, $type, $money, $coin, $waittime) {
        
        //24738341, failed
        
        $trankArray = array(24700231,
                            24700291,
                            24700351, 
                            24736891, 
                            24736921,
                            24736971,
                            24738091,
                            24738501,
                            24738521,
                            20220341,
                            20220351,
                            20220361,
                            20220371,
                            20220381,
                            20220391,
                            24700721,
                            24700781,
                            24700811,
                            24700891);
        
        $trunkid = rand(0, 18); 
        
        $originate  = "Action: originate\r\n";
        //$originate .= "Channel: Local/".$phone."@RANDOM\r\n";
        if (strlen((string)$phone) == 9) {
            $originate .= "Channel: SIP/". $trankArray[$trunkid] ."/0".$phone."\r\n";
        }
        $originate .= "Channel: SIP/". $trankArray[$trunkid] ."/".$phone."\r\n";
        $originate .= "Timeout: $waittime\r\n";
        $originate .= "Context: autoinformator\r\n";
        $originate .= "Callerid: AI-". $trankArray[$trunkid] ."-".$reqdetid."-".$phone."\r\n";
        $originate .= "Exten: 601\r\n";
        $originate .= "Variable: REQDETID=".$phone."\r\n";
        $originate .= "Variable: TYPE=".$type."\r\n";
        $originate .= "Variable: MONEY=".$money."\r\n";
        $originate .= "Variable: COIN=".$coin."\r\n";
        $originate .= "Async: true\r\n";
        $originate .= "ActionID: ".$reqdetid."\r\n";
        $originate .= "\r\n";
        
        fwrite($this->connect, $originate);
          
    }
            
    /**
     * 
     */
    function getExtensions() {
        
        $checkpeer = "Action: Command\r\n";
        $checkpeer .= "Command: core show hints \r\n";
        $checkpeer .= "\r\n";
        fwrite($this->connect, $checkpeer);
        $line = trim(fgets($this->connect));
        
        $found_entry = false;
        $table       = '<table id="box-table-b" style="float:left;">';
        $i=0;
        while ($line != "--END COMMAND--") {
            if (($i-4)%28==0) {$table.="</td><table style=\"float:left;\" id=\"box-table-b\">"; } ;
            switch (true) {
        
                case strpos($line,"State:Idle"):
                    $table .= '<tr>';
                    $table .= '<td>'.substr($line, 0, 3).'</td>';
                    $table .= '<td><font size="2" color="green">თავისუფალი</font></td>';
                    $table .= '</tr>';
                    break;
        
                case strpos($line,"State:InUse"):
                    $table .= '<tr>';
                    $table .= '<td>'.substr($line, 0, 3).'</td>';
                    $table .= '<td><font size="2" color="#CC9900">საუბრობს</font></td>';
                    $table .= '</tr>';
                    break;
        
                case strpos($line,"State:Unavailable"):
                    $table .= '<tr>';
                    $table .= '<td>'.substr($line, 0, 3).'</td>';
                    $table .= '<td><font size="2" color="red">მიუწვდომელი</font></td>';
                    $table .= '</tr>';
                    break;
        
                case strpos($line,"State:Unknow"):
                    $table .= '<tr>';
                    $table .= '<td>'.substr($line, 0, 3).'</td>';
                    $table .= '<td><font size="2" color="red">უცნობი</font></td>';
                    $table .= '</tr>';
                    break;
            }
        
            $i++;
             
            $line = trim(fgets($this->connect));
        }
        
        $table .= '</table>';
        echo $table;
        fclose($this->connect);
        exit(0);
        
    } 
    
    function getCannels() {
    
        $checkpeer = "Action: WaitEvent\r\n";
        $checkpeer .= "\r\n";
        fwrite($this->connect, $checkpeer);
        $line = trim(fgets($this->connect));
        
        $result = '';
        while ($line != "Event: WaitEventComplete") {
             
            $result .= trim(fgets($this->connect)).'</br>';
            
        }
    
        return  $result;
        
        fclose($this->connect);
        exit(0);
    
    }
    
    /**
     * @param unknown $phone
     */
    function blackListAdd($phone) {
    
        $command = "Action: Command\r\n";
        $command .= "Command: database put blacklist $phone \"test\" \r\n";
        $command .= "\r\n";
        fwrite($this->connect,$command);
        
        fclose($this->connect);
        exit(0);
    
    }
    
    /**
     * @param unknown $phone
     */
    function blackListRemove($phone) {
    
        $command = "Action: Command\r\n";
        $command .= "Command: database del blacklist $phone \r\n";
        $command .= "\r\n";
        fwrite($this->connect,$command);
    
        fclose($this->connect);
        exit(0);
    
    }
    
    /**
     * destructor
     */
    function __destruct() {
        fclose($this->connect);
    }
        
}
